<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Programming in Lua (first edition)</TITLE>
<LINK REL="stylesheet" TYPE="text/css" HREF="../lua.css">
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1">
<STYLE TYPE="text/css">
ul {
	list-style-type: none ;
}
</STYLE>
</HEAD>

<BODY>

<HR>
<H1>
<A HREF="../home.html"><IMG SRC="../images/logo.gif" ALT="Lua" BORDER=0></A>
Programming in Lua (first edition)
</H1>

This is the online version of the first edition of the book
<A HREF="./">Programming in Lua</A>,
a detailed and authoritative introduction to all aspects of Lua programming written by Lua's chief architect.
<P>

<BLOCKQUOTE>
<A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/lua-pilindex-20">
<IMG TITLE="buy at the Lua Bookstore at Amazon" SRC="capa.jpg" ALT="" BORDER=1 ALIGN="left" HSPACE=12></A>
<A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/lua-pilindex-20">
<font color="gray">Programming in</font>
<font color="blue">Lua</font></A>
<BR>by Roberto Ierusalimschy
<BR>Lua.org, December 2003
<BR>ISBN 8590379817
<BR CLEAR="all">
</BLOCKQUOTE>
<P>

The first edition was aimed at
<A HREF="../manual/5.0/">Lua 5.0</A>
and remains largely relevant.
<P>

If you find this online version useful,
please consider
<A HREF="../donations.html#books">buying a copy</A>
of the
<A HREF="./">current edition</A>.
This helps to
<A HREF="../donations.html">support the Lua project</A>.
<P>

<SMALL>
Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
<BR>
This online book is for personal use only.
It cannot be copied to other web sites or further distributed in any form.
</SMALL>
<P>

<H2>Contents</H2>
<UL style="padding: 0">
<LI><a name="0" href="p1.html">Preface</a>
<LI><a name="p1.1" href="p1.1.html">Audience</a>
<LI><a name="p1.2" href="p1.2.html">Other Resources</a>
<LI><a name="p1.3" href="p1.3.html">A Few Typographical Conventions</a>
<LI><a name="p1.4" href="p1.4.html">About the Book</a>
<LI><a name="p1.5" href="p1.5.html">Acknowledgments</a>
<P>
<LI><a name="P1" href="1.html"><b>Part I &middot; The Language</b></a>
<P>
<LI><a name="1" href="1.html">1 &ndash; Getting Started</a>
<UL>
<LI><a name="1.1" href="1.1.html">1.1 &ndash; Chunks</a>
<LI><a name="1.2" href="1.2.html">1.2 &ndash; Global Variables</a>
<LI><a name="1.3" href="1.3.html">1.3 &ndash; Some Lexical Conventions</a>
<LI><a name="1.4" href="1.4.html">1.4 &ndash; The Stand-Alone Interpreter</a>
</UL>
<P>
<LI><a name="2" href="2.html">2 &ndash; Types and Values</a>
<UL>
<LI><a name="2.1" href="2.1.html">2.1 &ndash; Nil</a>
<LI><a name="2.2" href="2.2.html">2.2 &ndash; Booleans</a>
<LI><a name="2.3" href="2.3.html">2.3 &ndash; Numbers</a>
<LI><a name="2.4" href="2.4.html">2.4 &ndash; Strings</a>
<LI><a name="2.5" href="2.5.html">2.5 &ndash; Tables</a>
<LI><a name="2.6" href="2.6.html">2.6 &ndash; Functions</a>
<LI><a name="2.7" href="2.7.html">2.7 &ndash; Userdata and Threads</a>
</UL>
<P>
<LI><a name="3" href="3.html">3 &ndash; Expressions</a>
<UL>
<LI><a name="3.1" href="3.1.html">3.1 &ndash; Arithmetic Operators</a>
<LI><a name="3.2" href="3.2.html">3.2 &ndash; Relational Operators</a>
<LI><a name="3.3" href="3.3.html">3.3 &ndash; Logical Operators</a>
<LI><a name="3.4" href="3.4.html">3.4 &ndash; Concatenation</a>
<LI><a name="3.5" href="3.5.html">3.5 &ndash; Precedence</a>
<LI><a name="3.6" href="3.6.html">3.6 &ndash; Table Constructors</a>
</UL>
<P>
<LI><a name="4" href="4.html">4 &ndash; Statements</a>
<UL>
<LI><a name="4.1" href="4.1.html">4.1 &ndash; Assignment</a>
<LI><a name="4.2" href="4.2.html">4.2 &ndash; Local Variables and Blocks</a>
<LI><a name="4.3" href="4.3.html">4.3 &ndash; Control Structures</a>
<UL>
<LI><a name="4.3.1" href="4.3.1.html">4.3.1 &ndash; <b>if</b> <b>then</b> <b>else</b></a>
<LI><a name="4.3.2" href="4.3.2.html">4.3.2 &ndash; <b>while</b></a>
<LI><a name="4.3.3" href="4.3.3.html">4.3.3 &ndash; <b>repeat</b></a>
<LI><a name="4.3.4" href="4.3.4.html">4.3.4 &ndash; Numeric <b>for</b></a>
<LI><a name="4.3.5" href="4.3.5.html">4.3.5 &ndash; Generic <b>for</b></a>
</UL>
<LI><a name="4.4" href="4.4.html">4.4 &ndash; <b>break</b> and <b>return</b></a>
</UL>
<P>
<LI><a name="5" href="5.html">5 &ndash; Functions</a>
<UL>
<LI><a name="5.1" href="5.1.html">5.1 &ndash; Multiple Results</a>
<LI><a name="5.2" href="5.2.html">5.2 &ndash; Variable Number of Arguments</a>
<LI><a name="5.3" href="5.3.html">5.3 &ndash; Named Arguments</a>
</UL>
<P>
<LI><a name="6" href="6.html">6 &ndash; More about Functions</a>
<UL>
<LI><a name="6.1" href="6.1.html">6.1 &ndash; Closures</a>
<LI><a name="6.2" href="6.2.html">6.2 &ndash; Non-Global Functions</a>
<LI><a name="6.3" href="6.3.html">6.3 &ndash; Proper Tail Calls</a>
</UL>
<P>
<LI><a name="7" href="7.html">7 &ndash; Iterators and the Generic <b>for</b></a>
<UL>
<LI><a name="7.1" href="7.1.html">7.1 &ndash; Iterators and Closures</a>
<LI><a name="7.2" href="7.2.html">7.2 &ndash; The Semantics of the Generic <b>for</b></a>
<LI><a name="7.3" href="7.3.html">7.3 &ndash; Stateless Iterators</a>
<LI><a name="7.4" href="7.4.html">7.4 &ndash; Iterators with Complex State</a>
<LI><a name="7.5" href="7.5.html">7.5 &ndash; True Iterators</a>
</UL>
<P>
<LI><a name="8" href="8.html">8 &ndash; Compilation, Execution, and Errors</a>
<UL>
<LI><a name="8.1" href="8.1.html">8.1 &ndash; The <code>require</code> Function</a>
<LI><a name="8.2" href="8.2.html">8.2 &ndash; C Packages</a>
<LI><a name="8.3" href="8.3.html">8.3 &ndash; Errors</a>
<LI><a name="8.4" href="8.4.html">8.4 &ndash; Error Handling and Exceptions</a>
<LI><a name="8.5" href="8.5.html">8.5 &ndash; Error Messages and Tracebacks</a>
</UL>
<P>
<LI><a name="9" href="9.html">9 &ndash; Coroutines</a>
<UL>
<LI><a name="9.1" href="9.1.html">9.1 &ndash; Coroutine Basics</a>
<LI><a name="9.2" href="9.2.html">9.2 &ndash; Pipes and Filters</a>
<LI><a name="9.3" href="9.3.html">9.3 &ndash; Coroutines as Iterators</a>
<LI><a name="9.4" href="9.4.html">9.4 &ndash; Non-Preemptive Multithreading</a>
</UL>
<P>
<LI><a name="10" href="10.html">10 &ndash; Complete Examples</a>
<UL>
<LI><a name="10.1" href="10.1.html">10.1 &ndash; Data Description</a>
<LI><a name="10.2" href="10.2.html">10.2 &ndash; Markov Chain Algorithm</a>
</UL>
<LI>&nbsp;
<P>
<LI><a name="P2" href="11.html"><b>Part II &middot; Tables and Objects</b></a>
<P>
<LI><a name="11" href="11.html">11 &ndash; Data Structures</a>
<UL>
<LI><a name="11.1" href="11.1.html">11.1 &ndash; Arrays</a>
<LI><a name="11.2" href="11.2.html">11.2 &ndash; Matrices and Multi-Dimensional Arrays</a>
<LI><a name="11.3" href="11.3.html">11.3 &ndash; Linked Lists</a>
<LI><a name="11.4" href="11.4.html">11.4 &ndash; Queues and Double Queues</a>
<LI><a name="11.5" href="11.5.html">11.5 &ndash; Sets and Bags</a>
<LI><a name="11.6" href="11.6.html">11.6 &ndash; String Buffers</a>
</UL>
<P>
<LI><a name="12" href="12.html">12 &ndash; Data Files and Persistence</a>
<UL>
<LI><a name="12.1" href="12.1.html">12.1 &ndash; Serialization</a>
<UL>
<LI><a name="12.1.1" href="12.1.1.html">12.1.1 &ndash; Saving Tables without Cycles</a>
<LI><a name="12.1.2" href="12.1.2.html">12.1.2 &ndash; Saving Tables with Cycles</a>
</UL>
</UL>
<P>
<LI><a name="13" href="13.html">13 &ndash; Metatables and Metamethods</a>
<UL>
<LI><a name="13.1" href="13.1.html">13.1 &ndash; Arithmetic Metamethods</a>
<LI><a name="13.2" href="13.2.html">13.2 &ndash; Relational Metamethods</a>
<LI><a name="13.3" href="13.3.html">13.3 &ndash; Library-Defined Metamethods</a>
<LI><a name="13.4" href="13.4.html">13.4 &ndash; Table-Access Metamethods</a>
<UL>
<LI><a name="13.4.1" href="13.4.1.html">13.4.1 &ndash; The <code>__index</code> Metamethod</a>
<LI><a name="13.4.2" href="13.4.2.html">13.4.2 &ndash; The <code>__newindex</code> Metamethod</a>
<LI><a name="13.4.3" href="13.4.3.html">13.4.3 &ndash; Tables with Default Values</a>
<LI><a name="13.4.4" href="13.4.4.html">13.4.4 &ndash; Tracking Table Accesses</a>
<LI><a name="13.4.5" href="13.4.5.html">13.4.5 &ndash; Read-Only Tables</a>
</UL>
</UL>
<P>
<LI><a name="14" href="14.html">14 &ndash; The Environment</a>
<UL>
<LI><a name="14.1" href="14.1.html">14.1 &ndash; Accessing Global Variables with Dynamic Names</a>
<LI><a name="14.2" href="14.2.html">14.2 &ndash; Declaring Global Variables</a>
<LI><a name="14.3" href="14.3.html">14.3 &ndash; Non-Global Environments</a>
</UL>
<P>
<LI><a name="15" href="15.html">15 &ndash; Packages</a>
<UL>
<LI><a name="15.1" href="15.1.html">15.1 &ndash; The Basic Approach</a>
<LI><a name="15.2" href="15.2.html">15.2 &ndash; Privacy</a>
<LI><a name="15.3" href="15.3.html">15.3 &ndash; Packages and Files</a>
<LI><a name="15.4" href="15.4.html">15.4 &ndash; Using the Global Table</a>
<LI><a name="15.5" href="15.5.html">15.5 &ndash; Other Facilities</a>
</UL>
<P>
<LI><a name="16" href="16.html">16 &ndash; Object-Oriented Programming</a>
<UL>
<LI><a name="16.1" href="16.1.html">16.1 &ndash; Classes</a>
<LI><a name="16.2" href="16.2.html">16.2 &ndash; Inheritance</a>
<LI><a name="16.3" href="16.3.html">16.3 &ndash; Multiple Inheritance</a>
<LI><a name="16.4" href="16.4.html">16.4 &ndash; Privacy</a>
<LI><a name="16.5" href="16.5.html">16.5 &ndash; The Single-Method Approach</a>
</UL>
<P>
<LI><a name="17" href="17.html">17 &ndash; Weak Tables</a>
<UL>
<LI><a name="17.1" href="17.1.html">17.1 &ndash; Memoize Functions</a>
<LI><a name="17.2" href="17.2.html">17.2 &ndash; Object Attributes</a>
<LI><a name="17.3" href="17.3.html">17.3 &ndash; Revisiting Tables with Default Values</a>
</UL>
<LI>&nbsp;
<P>
<a name="P3" href="18.html"><b>Part III &middot; The Standard Libraries</b></a>
<P>
<LI><a name="18" href="18.html">18 &ndash; The Mathematical Library</a>
<P>
<LI><a name="19" href="19.html">19 &ndash; The Table Library</a>
<UL>
<LI><a name="19.1" href="19.1.html">19.1 &ndash; Array Size</a>
<LI><a name="19.2" href="19.2.html">19.2 &ndash; Insert and Remove</a>
<LI><a name="19.3" href="19.3.html">19.3 &ndash; Sort</a>
</UL>
<P>
<LI><a name="20" href="20.html">20 &ndash; The String Library</a>
<UL>
<LI><a name="20.1" href="20.1.html">20.1 &ndash; Pattern-Matching Functions</a>
<LI><a name="20.2" href="20.2.html">20.2 &ndash; Patterns</a>
<LI><a name="20.3" href="20.3.html">20.3 &ndash; Captures</a>
<LI><a name="20.4" href="20.4.html">20.4 &ndash; Tricks of the Trade</a>
</UL>
<P>
<LI><a name="21" href="21.html">21 &ndash; The I/O Library</a>
<UL>
<LI><a name="21.1" href="21.1.html">21.1 &ndash; The Simple I/O Model</a>
<LI><a name="21.2" href="21.2.html">21.2 &ndash; The Complete I/O Model</a>
<UL>
<LI><a name="21.2.1" href="21.2.1.html">21.2.1 &ndash; A Small Performance Trick</a>
<LI><a name="21.2.2" href="21.2.2.html">21.2.2 &ndash; Binary Files</a>
</UL>
<LI><a name="21.3" href="21.3.html">21.3 &ndash; Other Operations on Files</a>
</UL>
<P>
<LI><a name="22" href="22.html">22 &ndash; The Operating System Library</a>
<UL>
<LI><a name="22.1" href="22.1.html">22.1 &ndash; Date and Time</a>
<LI><a name="22.2" href="22.2.html">22.2 &ndash; Other System Calls</a>
</UL>
<P>
<LI><a name="23" href="23.html">23 &ndash; The Debug Library</a>
<UL>
<LI><a name="23.1" href="23.1.html">23.1 &ndash; Introspective Facilities</a>
<UL>
<LI><a name="23.1.1" href="23.1.1.html">23.1.1 &ndash; Accessing Local Variables</a>
<LI><a name="23.1.2" href="23.1.2.html">23.1.2 &ndash; Accessing Upvalues</a>
</UL>
<LI><a name="23.2" href="23.2.html">23.2 &ndash; Hooks</a>
<LI><a name="23.3" href="23.3.html">23.3 &ndash; Profiles</a>
</UL>
<LI>&nbsp;
<P>
<a name="P4" href="24.html"><b>Part IV &middot; The C API</b></a>
<P>
<LI><a name="24" href="24.html">24 &ndash; An Overview of the C API</a>
<UL>
<LI><a name="24.1" href="24.1.html">24.1 &ndash; A First Example</a>
<LI><a name="24.2" href="24.2.html">24.2 &ndash; The Stack</a>
<UL>
<LI><a name="24.2.1" href="24.2.1.html">24.2.1 &ndash; Pushing Elements</a>
<LI><a name="24.2.2" href="24.2.2.html">24.2.2 &ndash; Querying Elements</a>
<LI><a name="24.2.3" href="24.2.3.html">24.2.3 &ndash; Other Stack Operations</a>
</UL>
<LI><a name="24.3" href="24.3.html">24.3 &ndash; Error Handling with the C API</a>
<UL>
<LI><a name="24.3.1" href="24.3.1.html">24.3.1 &ndash; Error Handling in Application Code</a>
<LI><a name="24.3.2" href="24.3.2.html">24.3.2 &ndash; Error Handling in Library Code</a>
</UL>
</UL>
<P>
<LI><a name="25" href="25.html">25 &ndash; Extending your Application</a>
<UL>
<LI><a name="25.1" href="25.1.html">25.1 &ndash; Table Manipulation</a>
<LI><a name="25.2" href="25.2.html">25.2 &ndash; Calling Lua Functions</a>
<LI><a name="25.3" href="25.3.html">25.3 &ndash; A Generic Call Function</a>
</UL>
<P>
<LI><a name="26" href="26.html">26 &ndash; Calling C from Lua</a>
<UL>
<LI><a name="26.1" href="26.1.html">26.1 &ndash; C Functions</a>
<LI><a name="26.2" href="26.2.html">26.2 &ndash; C Libraries</a>
</UL>
<P>
<LI><a name="27" href="27.html">27 &ndash; Techniques for Writing C Functions</a>
<UL>
<LI><a name="27.1" href="27.1.html">27.1 &ndash; Array Manipulation</a>
<LI><a name="27.2" href="27.2.html">27.2 &ndash; String Manipulation</a>
<LI><a name="27.3" href="27.3.html">27.3 &ndash; Storing State in C Functions</a>
<UL>
<LI><a name="27.3.1" href="27.3.1.html">27.3.1 &ndash; The Registry</a>
<LI><a name="27.3.2" href="27.3.2.html">27.3.2 &ndash; References</a>
<LI><a name="27.3.3" href="27.3.3.html">27.3.3 &ndash; Upvalues</a>
</UL>
</UL>
<P>
<LI><a name="28" href="28.html">28 &ndash; User-Defined Types in C</a>
<UL>
<LI><a name="28.1" href="28.1.html">28.1 &ndash; Userdata</a>
<LI><a name="28.2" href="28.2.html">28.2 &ndash; Metatables</a>
<LI><a name="28.3" href="28.3.html">28.3 &ndash; Object-Oriented Access</a>
<LI><a name="28.4" href="28.4.html">28.4 &ndash; Array Access</a>
<LI><a name="28.5" href="28.5.html">28.5 &ndash; Light Userdata</a>
</UL>
<P>
<LI><a name="29" href="29.html">29 &ndash; Managing Resources</a>
<UL>
<LI><a name="29.1" href="29.1.html">29.1 &ndash; A Directory Iterator</a>
<LI><a name="29.2" href="29.2.html">29.2 &ndash; An XML Parser</a>
</UL>
</UL>
<P>

<HR>
<SMALL CLASS="footer">
Last update:
Wed May 15 09:13:48 BRT 2013
</SMALL>
<!--
Last change: minor edit
-->

</BODY>
</HTML>
